package study.queue;



/**
 * @Classname LinkedListQueue
 * @Author happytaohaha
 * @Date 2020/3/28
 * @Description TODO
 */
public class LinkedListQueue<E> implements Queue<E> {
    /**
     * 内部类 节点类
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head,tail;
    private int size;

    public LinkedListQueue(){
        head =null;
        tail =null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void enqueue(E e) {
        if(tail==null){
            tail = new Node(e);
            head = tail;
        }else{
            tail.next = new Node(e);
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E dequeue() {
        if(isEmpty()){
            throw new IllegalArgumentException("队列为空，无法出队");
        }
        Node retNode = head;
        head = head.next;
        retNode.next = null;
        if(head ==null){
            tail = null;
        }
        return retNode.e;
    }

    @Override
    public E getFront() {
        if(isEmpty()){
            throw new IllegalArgumentException("队列为空，无法出队");
        }
        return head.e;
    }

    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        res.append("Queue: front");
        res.append("[");
      Node cur =head;
      while (head!=null){
          res.append(cur+"->");
          cur = cur.next;
      }
        res.append("]  tail");
        return res.toString();
    }
}
